<template>
  <div class="hello">
    <el-row :gutter="20">
      <el-col :span="6">
        <el-button type="primary" @click="clickBtn">{{ msg }}</el-button>
      </el-col>
      <el-col :span="6">
        <el-button @click="returnIndex">跳转到默认的Helloworld页面</el-button>
      </el-col>
      <el-col :span="6">
        <el-button type="success" @click="queryVersion">查询版本号</el-button>
      </el-col>
      <el-col :span="6">
        <el-input placeholder="当前暂无版本号" v-model="version.info" :disabled="true"></el-input>
      </el-col>
    </el-row>
    <br/>
    <el-row>
      <el-col :span="6">
        <el-button-group>
          <el-button type="primary" :disabled="true" icon="el-icon-share"></el-button>
          <el-button type="primary" @click="authorization('1')">百度账号登录</el-button>
        </el-button-group>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  $(function () {
    console.log('welcome');
  })
  export default {
    name: "hello",
    data() {
      return {
        msg: "ElementUI-Button",
        version: {
          info: '',
          param: ''
        },
        authorizationCode: ''//三方oauth系统的code
      }
    },
    methods: {
      authorization(type) {
        console.log("进行鉴权");
        this.$http.post("proxy/unionAuth/authorizationCode", {authorizeType: type}).then(response => {
          let data = response.data;
          console.log(data.url)
          // this.$router.push(this.$route.query.redirect || data.url);//内部页面重定向
          window.location.href = data.url;
        });
      },
      clickBtn() {
        alert("你点我干嘛");
      },
      returnIndex() {
        this.$router.push({path: '/'});
      },
      queryVersion() {
        if (this.version.info == null) {
          this.version.param = '1';
        }
        this.$http.post("proxy/unionAuth/version", {param: this.version.param}).then(successData => {
          this.version.info = successData.data.version;
          console.log(successData.data)
        });
      },
      accessToken(code) {
        if (code == 'empty') {
          console.log('empty code');
          return;
        }

        this.$http.post("proxy/unionAuth/accessTokenByCode", this.$queryParam.stringify({'code':code,'requestOs':'Baidu'})).then(response => {
          console.log(response.data);
        })

      }
    },
    created() {
      let code = this.$route.query.code;
      if (code == null) {
        const h = this.$createElement;
        this.$notify({
          title: '警告',
          message: h('i', {style: 'color : teal'}, '页面跳转不合法，未携带code参数')
        });
      } else {
        this.authorizationCode = code;
        console.log(this.authorizationCode);
        this.accessToken(this.authorizationCode);
      }
    }

  }
</script>
